package com.sfh.cpc.home.menu;

import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;

import com.sfh.cpc.R;
import com.sfh.cpc.home.fragment.UserCharmPageFragment;
import com.sfh.cpc.home.fragment.NoteCityPageFragment;
import com.sfh.cpc.home.fragment.NoteHotPageFragment;
import com.sfh.cpc.home.fragment.NoteNewPageFragment;
import com.sfh.cpc.home.fragment.UserWealthPageFragment;
import com.sfh.lib.ui.AbstractLifecycleFragment;

import java.util.ArrayList;
import java.util.List;

/**
 * 功能描述: 今日福利
 *
 * @author SunFeihu 孙飞虎
 * @date 2018/8/6
 */
public class WelfareFragment extends AbstractLifecycleFragment {
    TabLayout tabLayout;
    ViewPager viewPager;

    @Override
    public int getLayout() {
        return R.layout.welfare_fragment;
    }


    @Override
    public void initData(View view) {
        this.tabLayout = view.findViewById(R.id.tab_layout);
        this.viewPager = view.findViewById(R.id.view_pager);

        Adapter adapter = new Adapter(getChildFragmentManager());
        this.viewPager.setAdapter(adapter);
        this.viewPager.setOffscreenPageLimit(1);
        this.tabLayout.setupWithViewPager( this.viewPager);
    }

    class Adapter extends FragmentPagerAdapter {

        List<AbstractLifecycleFragment> fragments = new ArrayList<>(2);

        @Nullable
        @Override
        public CharSequence getPageTitle(int position) {
            return fragments.get(position).getName();
        }

        public Adapter(FragmentManager fm) {
            super(fm);
            fragments.add(NoteHotPageFragment.newFragment());
            fragments.add(NoteNewPageFragment.newFragment());
            fragments.add(NoteCityPageFragment.newFragment());
            fragments.add(UserWealthPageFragment.newFragment());
            fragments.add(UserCharmPageFragment.newFragment());
        }

        @Override
        public Fragment getItem(int position) {
            return fragments.get(position);
        }

        @Override
        public int getCount() {
            return fragments.size();
        }
    }
}
